<?php

namespace app\api\controller;

use app\api\controller\Ltd;
use think\Db;
/**
 * 报备📂
 * 
 */
class Need extends Ltd
{


    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['p'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 添加报备
     * @param string $goods_ids 房源id多个用,拼接
     * @param string $name 姓名
     * @param string $tel 电话
     * @param string $ps 备注
     * @param string $d_time 到访时间
     * @param string $b_time 报备时间
     * @param string $sex 性别:1=男,2=女,0=未知
     * @param string $areas 南北东西
     * @ApiSummary ("0=报备1=未联系上2=已联系上3=预定成功4=暂无需求5=完结")
     * @return void
     */
    public function add_need()
    {
        $this->is_staff();

        $params = $this->request->param();
        // halt($params);
        $fields = ['goods_ids','name','tel','ps','b_time','d_time','sex','areas'];
        $data = $this->get_field_arr($params,$fields,true);
        // halt($data);
  
        if(!isset($data['b_time'])){
            $data['b_time'] = time();
        }

        $data['pid'] =  $this->auth->id;
        $data['admin_id'] = $this->get_admin_id();

        $data3 = $data;
        unset($data3['goods_ids']);
        unset($data3['areas']);
        unset($data3['b_time']);
        unset($data3['d_time']);
        unset($data3['ps']);

        
        $res = Db::name('clients')->where($data3)->find();
        
        //添加客户

        $data2 = $this->get_field_arr($data,['name','tel','sex','admin_id','pid'],true);
        $data['clients_id'] = $this->AddClients($data2);


        // dump($data);
        unset($data['name']);
        unset($data['tel']);
        unset($data['sex']);
        // halt($data);

        if($res){
            $this->error('已有该用户',$res);
        }else{
            $data['status'] = '0';
            $data['time'] = time();

            $id = Db::name('need')->insertGetId($data);

            if($id)$this->success('提交成功');

        }
    }

    /**
     * 获取提交的报备列表🌈
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $status null全部0=报备1=未联系上2=已联系上3=预定成功4=暂无报备5=完结
     * @return void
     */
    public function get_need_list($page=1,$limit=10,$status=null)
    {
        $data['pid'] = $this->auth->id;
        if($status){
            $data['status'] = $status;
        }
        // $data['read_status'] = '1';//阅读状态:0=待处理,1=通过,2=拒绝
        $data['need_status'] = '1';//还未跟进 的

        $list = Db::name('need')
                ->where($data)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
        foreach($list as $key=>&$val){
            // $val['goodcate'] = $this->GoodscateIdsToArray($val['goodscate_ids']);
            $val['goods_list'] = $this->GoodsIdsToArray($val['goods_ids']);
            $val['clients'] = $this->GetClients($val['clients_id']);
        }
        $this->success('ok',$list);
    }


    /**
     * 获取报备信息🌈
     *
     * @param int $id id
     * @return void
     */
    public function get_need_data($id)
    {
        $data['pid'] = $this->auth->id;
        $data['id'] = $id;
        $res = Db::name('need')
            ->where($data)
            ->find();
        if($res){
            $res['goods_list']= $this->GoodsIdsToArray($res['goods_ids']);
            $res['clients'] = $this->GetClients($res['clients_id']);
        }

        $this->success('ok',$res);
    }

    /**
     * 编辑报备信息
     *
     * @param int $id id
     * @param string $d_time 到访时间
     * @ApiSummary ("'d_time','name','tel',ps")
     * @return void
     */
    public function edit_need($id)
    {
        $this->is_staff();
        $params = $this->request->param();

        $data['pid'] = $this->auth->id;
        $res = Db::name('need')->where($data)->where('id',$id)->find();
        $fields = ['d_time','b_time','ps'];
        $data = $this->get_field_arr($params,$fields,true);

        
        
        
        if($res){
          
            $data['updatetime'] = time();
            $res = Db::name('need')->where('id',$id)->update($data);

            if($res)$this->success('修改成功');
        }else{
            $this->error('没有该报备');
           

        }
    }




    /**
     * 添加用户需求（跟进
     * @ApiSummary ("'goods_id','name','house_status','ps','area_size','area','house_type','tag_ids','price','clients_id'")
     * @return void
     */
    public function add_clients_need()
    {
        $params = $this->request->param();
        $fields = ['goods_id','name','house_status','ps','area_size','area','house_type','tag_ids','price','clients_id'];
        
        $data = $this->get_field_arr($params,$fields,true);
        $data['admin_id'] = $this->get_admin_id();
       
        $data['pid'] = $this->auth->id;

        $res = Db::name('clients_need')->where($data)->find();
        if($res){
            $this->error('已有该记录');
        }

        $data['time'] = time();
        $id = Db::name('clients_need')->insertGetId($data);
        if($id){
            Db::name('need')->where('clients_id',$data['clients_id'])->update(['need_status'=>'2']);
            $this->success('添加成功');
        }

    }


    /**
     * 获取客户需求列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $status 状态:1=未成交,2=成交
     * @return void
     */
    public function get_clients_need_list($page=1,$limit=10,$status=null)
    {
        $where = null;
        if($status){
            $where['status'] = $status;
        }

        $list = Db::name('clients_need')
                ->where('pid',$this->auth->id)
                ->where($where)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
            
        foreach ($list as $key => &$val) {
            $val['goods'] = $this->GetGoodsData2($val['goods_id']); 
            $val['clients'] = $this->GetClients($val['clients_id']);
            $val['tags'] = $this->TagIdsToArray($val['tag_ids']);
        }

        $this->success('ok',$list);
    }


    /**
     * 获取客户需求信息
     *
     * @param int $id id
     * @return void
     */
    public function get_clients_need_data($id)
    {
        $val = Db::name('clients_need')
                ->where('pid',$this->auth->id)
                // ->order('time desc')
                // ->page($page,$limit)
                ->where('id',$id)
                ->find();
        $val['goods'] = $this->GetGoodsData2($val['goods_id']); 
        $val['clients'] = $this->GetClients($val['clients_id']);
        $val['tags'] = $this->TagIdsToArray($val['tag_ids']);

        $this->success('ok',$val);
    }


    /**
     * 获取客户需求信息
     *
     * @param int $id id
     * @return void
     */
    public function edit_clients_need_data($id)
    {
        $params = $this->request->param();
        $fields = ['name','house_status','ps','area_size','area','house_type','tag_ids','price','clients_id'];
        
        $data = $this->get_field_arr($params,$fields,true);
        // $data['admin_id'] = $this->get_admin_id();
       
        // $data['pid'] = $this->auth->id;

        $res = Db::name('clients_need')
            ->where('id',$id)
            ->find();
        if(!$res){
            $this->error('没有该记录');
        }
        // $data['time'] = time();
        $id = Db::name('clients_need')
            ->where('id',$id)
            ->update($data);
        if($id){
            $this->success('修改成功');
        }
    }


    
}
